package com.company.cloud.app.modules.config;

import com.company.cloud.common.auth.bean.WebRequestUrlModel;
import com.company.cloud.common.auth.utils.WebRequestUrlUtils;
import com.company.cloud.common.constant.ConfigConstants;
import com.company.cloud.user.modules.api.request.ServiceUrlConfigAddRequest;
import com.company.cloud.user.modules.api.service.UrlAuthPlatformRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.List;

@Configuration
@ConditionalOnProperty(value = ConfigConstants.CONFIG_Auth_URL_INIT_ENABLED, havingValue = "true")
public class UrlAuthInitConfig {

    @Component
    public class UrlAuthInitStarter implements CommandLineRunner {

        @Autowired
        private RequestMappingHandlerMapping requestMappingHandlerMapping;

        @Autowired
        private UrlAuthPlatformRemoteService urlAuthPlatformRemoteService;

        @Value("${spring.application.name}")
        private String serviceName;

        @Override
        public void run(String... args) throws Exception {
            System.out.println("+++++++++++++ 资源Url初始化AuthInitStarter serviceName : "+this.serviceName);
			List<WebRequestUrlModel> urls= WebRequestUrlUtils.getWebRequestUrls(requestMappingHandlerMapping);
			ServiceUrlConfigAddRequest params=new ServiceUrlConfigAddRequest();
			params.setServiceName(serviceName);
			params.setServicePrefix("");
			params.setServiceUrls(urls);
			urlAuthPlatformRemoteService.addAuthUrlConfigBatch(params);
        }


        public String getServiceName() {
            return serviceName;
        }


        public void setServiceName(String serviceName) {
            this.serviceName = serviceName;
        }


    }

}
